<?php
//enabling cookies for this page
session_start();
?>

<html>
<head>
<meta http-equiv="refresh" content="10">
<style type="text/css" media="only screen and (max-device-width: 480px)">
table {width:100%; font-family:Arial, Helvetica, sans-serif;}
a {font-family:Arial, Helvetica, sans-serif; font-size:80px; font-weight:bold;}
font {font-size:55px;}
td {width:65%;}
input {width:90; height:100}
.font {font-size:5;}
</style>
</head>
<body>

<?php
/*
* PatientPrepStatus.php gets information from the db's patientPreparationInformation table and displays it
* to the user while also allowing the statuses of those entries to be changed.
*/

/************************
* User variables to set.
* $completed - the background color, in hexidecimal, that will display for a field marked as completed
* $inProgress - the background color that will display for a field marked as in progress
* $problem- the background color that will display for a field marked as incomplete/having a problem
************************/
$completed  = "#00FF66";
$inProgress ="#1589FF";
$problem = "#FFFFcc";

//setting up the DB connection
$_SESSION['dbhost'] = 'engr-db.engr.oregonstate.edu:3307';
$_SESSION['dbname'] = 'funkecd';
$_SESSION['dbuser'] = 'funkecd';
$_SESSION['dbpass'] = 'ktrDtj1b';

$mysql_handle = mysql_connect($_SESSION['dbhost'], $_SESSION['dbuser'], $_SESSION['dbpass'])
    or die("Error connecting to database server");

mysql_select_db($_SESSION['dbname'], $mysql_handle)
    or die("Error selecting database: $dbname");
	
//internal variables
$status = array();
$extra = 0;
$i = 0;

//echos that set up the submission form of the page. 
echo '<font face= Arial, Helvetica, sans-serif size = "6">';
echo 'Patient Checklist</font><br>';
echo '<meta http-equiv="refresh" content="10">';
echo '<form method="post" action="index.php?page=PatientPrepStatus.php">';
echo '<font face= Arial, Helvetica, sans-serif>';
echo '<table border="1" style="border-collapse: collapse" width="70%">';

//for loop that checks to see if there needs to be a third colum drawn for reference links
for($i = 1; $i <= 11; $i++)
{
	//psid(id), status, ppid, pid
	$query = "SELECT * FROM `Patient_Status_Procedure` where ppid = $i";
	$result = mysql_query($query);
	$patient[$i] = mysql_fetch_array($result);

	//ppid(id), etid, step_name
	$query = "SELECT * FROM `Patient_Preparation_Steps` where ppid = $i";
	$result = mysql_query($query);
	$Arow[$i] = mysql_fetch_array($result);

	if($Arow[$i]['reference_flag'] == 1)
	{
		$extra = 1;
	}
}

//for loop that updates the db based on button presses and draws the table
for($i = 1; $i <= 11; $i++)
{
	//internal variables used to hold the 'names' of the different buttons
	$j = $i * 3;
	$k = $j + 1;
	$l = $k + 1;
	
	//if block that checks to see which button was pressed and updates the db appropriately
	if($_POST["$j"])
	{
		$query = "UPDATE  `Patient_Status_Procedure` 
		SET  `status` =  '1' 
		WHERE  `Patient_Status_Procedure`.`ppid` = $i LIMIT 1 ;";
		$result = mysql_query($query);
		$_POST["$j"] = null;
	}
	else if ($_POST["$k"])
	{	
		$query = "UPDATE  `Patient_Status_Procedure` 
		SET  `status` =  '2' 
		WHERE  `Patient_Status_Procedure`.`ppid` = $i LIMIT 1 ;";
		$result = mysql_query($query);
		$_POST["$k"] = null;
	}
	else if ($_POST["$l"])
	{
		$query = "UPDATE  `Patient_Status_Procedure` 
		SET  `status` =  '3' 
		WHERE  `Patient_Status_Procedure`.`ppid` = $i LIMIT 1 ;";
		$result = mysql_query($query);
		$_POST["$l"] = null;
	}

	//psid(id), status, ppid, pid
	$query = "SELECT * FROM `Patient_Status_Procedure` where ppid = $i";
	$result = mysql_query($query);
	$patient[$i] = mysql_fetch_array($result);

	//ppid(id), etid, step_name
	$query = "SELECT * FROM `Patient_Preparation_Steps` where ppid = $i";
	$result = mysql_query($query);
	$row[$i] = mysql_fetch_array($result);
	$status=$patient[$i]['status'];
	$step = $row[$i]['step_name'];

	if($row[$i]['reference_flag'] == 1)
	{
		$rLink = $row[$i]['reference_link'];
		$rText = $row[$i]['reference_text'];
		//this link  disappears on the iphone because of the formatting issues. 
		$ref = "<a class =\"font\" href=\"$rLink\"> $rText </a>";
	}
	else
	{
		$ref = "";
	}
	
	//setting all the buttons "up" by default
	$good = "goodup.png";
	$bad = "badup.png";
	$mid = "midup.png";
	
	//if block that sets the display status of the step	
	if ($status == 1) //unavilable
	{
		$bg = $problem;
		$bad = "baddown.png";		
	}
	else if($status == 2) //on route
	{
		$bg = $inProgress;
		$mid = "middown.png";		
	}
	else if ($status == 3)  //in house
	{
		$bg = $completed ;
		$good = "gooddown.png";
	}
	else //status not yet set
	{
		$bg = "white";
	}
	
	//set up and printing of the cell at this step
	$tag = "<td bgcolor=\"$bg\" align = \"left\"><font size=\"3\"><b>";
	$btag = "<td bgcolor=\"$bg\" align = \"top\">";
	$buttons = "<input type =\"image\" src=\"$bad\" NAME =\"$j\" Value =\"1\" width=\"24\" height = \"24\">
				<input type =\"image\" src=\"$mid\" NAME =\"$k\" Value =\"1\" width=\"24\" height = \"24\">
				<input type =\"image\" src=\"$good\" NAME =\"$l\" Value =\"1\" width=\"24\" height = \"24\">";

	//drawing the extra collum when required. 
	if($extra == 1) 
	{
		$refD = "<td bgcolor=\"$bg\" align = \"left\"><font size=\"3\">";
	}
	else  //otherwise, not
	{
		$refD = "";
		$ref = "";
	}
	
	echo "<tr>$tag $step $btag $buttons $refD $ref";
}

//closing the HTML and DB connection
echo"</table></form>";
echo '</font>';
mysql_close($mysql_handle);

?>
